import { create } from 'zustand';
import { AuthUser, User } from '@/types';

// 认证状态管理
interface AuthStore {
  user: AuthUser | null;
  isAuthenticated: boolean;
  login: (user: AuthUser) => void;
  logout: () => void;
}

export const useAuthStore = create<AuthStore>((set) => ({
  user: {
    id: '1',
    username: '系统管理员',
    role: 'admin',
    permissions: ['*'],
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=admin',
    department: '技术部',
  },
  isAuthenticated: true,
  login: (user) => set({ user, isAuthenticated: true }),
  logout: () => set({ user: null, isAuthenticated: false }),
}));

// 应用全局状态管理
interface AppStore {
  collapsed: boolean; // 侧边栏折叠状态
  loading: boolean; // 全局loading状态
  toggleCollapsed: () => void;
  setLoading: (loading: boolean) => void;
}

export const useAppStore = create<AppStore>((set) => ({
  collapsed: false,
  loading: false,
  toggleCollapsed: () => set((state) => ({ collapsed: !state.collapsed })),
  setLoading: (loading) => set({ loading }),
}));

// 用户数据状态管理
interface UserStore {
  users: User[];
  selectedUser: User | null;
  setUsers: (users: User[]) => void;
  setSelectedUser: (user: User | null) => void;
  updateUser: (userId: string, updates: Partial<User>) => void;
  removeUser: (userId: string) => void;
}

export const useUserStore = create<UserStore>((set, get) => ({
  users: [],
  selectedUser: null,
  setUsers: (users) => set({ users }),
  setSelectedUser: (selectedUser) => set({ selectedUser }),
  updateUser: (userId, updates) => set((state) => ({
    users: state.users.map(user => 
      user.id === userId ? { ...user, ...updates } : user
    )
  })),
  removeUser: (userId) => set((state) => ({
    users: state.users.filter(user => user.id !== userId),
    selectedUser: state.selectedUser?.id === userId ? null : state.selectedUser
  })),
}));